package com.common.util.repay_plan;

import com.common.util.date.DateUtil;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;

/**
 * @describe: 按月付息到期还本
 */
class DueToPayInterestOnSchedule {

    /**
     * 每期利息 = 借款金额 * 借款年利率/年化天数 * 本期借款天数
     *
     * @param repayPlanCondition 还款计划计算需要的条件
     * @param periodsUnit 还款期数单位
     * @return
     */
    protected static ArrayList<RepayPlan> getPlan(RepayPlanCondition repayPlanCondition, RepayDateUtil.PeriodsUnit periodsUnit) {

        Date startTime = repayPlanCondition.getStartTime();   //计息开始时间
        int loanDays = repayPlanCondition.getLoanDays();     //借款天数
        int fixedRepayDay = repayPlanCondition.getFixedRepayDay();   //固定还款日
        BigDecimal amt = repayPlanCondition.getAmt();      //借款总金额
        BigDecimal rate = repayPlanCondition.getRate();      //年化利率
        int annualNumberOfDays = repayPlanCondition.getAnnualNumberOfDays();  //年化天数

        ArrayList<Date> repayDate = RepayDateUtil.getRepayDate(startTime, loanDays, fixedRepayDay, periodsUnit);
        int periods = repayDate.size();
        ArrayList<RepayPlan> repayPlanList = new ArrayList<>(periods);
        if (periods == 1) {
            RepayPlan repayPlan = new RepayPlan();
            repayPlan.setStartDate(startTime);
            repayPlan.setEndDate(repayDate.get(0));
            repayPlan.setPrincipal(amt);

            BigDecimal interest = amt.multiply(BigDecimal.valueOf(loanDays))
                                     .multiply(rate)
                                     .divide(BigDecimal.valueOf(annualNumberOfDays), 2, BigDecimal.ROUND_HALF_UP);
            repayPlan.setInterest(interest);
            repayPlan.setLastPrincipal(amt);
            repayPlan.setInterestDays(loanDays);
            repayPlanList.add(repayPlan);
        } else {
            for (int i = 0; i < periods; i++) {
                RepayPlan repayPlan = new RepayPlan();
                if (i == 0) {
                    repayPlan.setStartDate(startTime);
                } else {
                    Date startDate = DateUtil.getBeginTime(DateUtil.addMillSecond(repayDate.get(i - 1), 1));
                    repayPlan.setStartDate(startDate);
                }
                repayPlan.setEndDate(repayDate.get(i));
                loanDays = DateUtil.getIntervalDays_1(repayPlan.getEndDate(), repayPlan.getStartDate());
                repayPlan.setInterestDays(loanDays);
                if (i == periods - 1) {  //最后一期
                    repayPlan.setLastPrincipal(amt);
                    repayPlan.setPrincipal(amt);
                    BigDecimal interest = amt.multiply(BigDecimal.valueOf(loanDays))
                                             .multiply(rate)
                                             .divide(BigDecimal.valueOf(annualNumberOfDays), 2, BigDecimal.ROUND_HALF_UP);
                    repayPlan.setInterest(interest);
                } else {
                    repayPlan.setLastPrincipal(amt);
                    repayPlan.setPrincipal(BigDecimal.ZERO);
                    BigDecimal interest = amt.multiply(BigDecimal.valueOf(loanDays))
                                             .multiply(rate)
                                             .divide(BigDecimal.valueOf(annualNumberOfDays), 2, BigDecimal.ROUND_HALF_UP);
                    repayPlan.setInterest(interest);
                }
                repayPlanList.add(repayPlan);
            }
        }
        return repayPlanList;
    }

}
